package com.pilates

import com.pilates.types.TypeAtivo;

class MatriculaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [matriculaInstanceList: Matricula.list(params), matriculaInstanceTotal: Matricula.count()]
    }

    def create = {
        def matriculaInstance = new Matricula()
        matriculaInstance.properties = params
        return [matriculaInstance: matriculaInstance]
    }

    def save = {
        def matriculaInstance = new Matricula(params)
        if (matriculaInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'matricula.label', default: 'Matricula'), matriculaInstance.id])}"
			redirect(action: "edit", id: matriculaInstance.id)
        }
        else {
			render(view: "edit", model: [matriculaInstance: matriculaInstance])
        }
		atualizaNumeroAlunos(matriculaInstance)
		
    }

    def show = {
        def matriculaInstance = Matricula.get(params.id)
        if (!matriculaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'matricula.label', default: 'Matricula'), params.id])}"
            redirect(action: "list")
        }
        else {
            [matriculaInstance: matriculaInstance]
        }
    }

    def edit = {
        def matriculaInstance = Matricula.get(params.id)
        if (!matriculaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'matricula.label', default: 'Matricula'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [matriculaInstance: matriculaInstance]
        }
    }

    def update = {
        def matriculaInstance = Matricula.get(params.id)
        if (matriculaInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (matriculaInstance.version > version) {
                    
                    matriculaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'matricula.label', default: 'Matricula')] as Object[], "Another user has updated this Matricula while you were editing")
                    render(view: "edit", model: [matriculaInstance: matriculaInstance])
                    return
                }
            }
            matriculaInstance.properties = params
            if (!matriculaInstance.hasErrors() && matriculaInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'matricula.label', default: 'Matricula'), matriculaInstance.id])}"
				atualizaNumeroAlunos(matriculaInstance)
				redirect(action: "edit", id: matriculaInstance.id)
            }
            else {
                render(view: "edit", model: [matriculaInstance: matriculaInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'matricula.label', default: 'Matricula'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def matriculaInstance = Matricula.get(params.id)
        if (matriculaInstance) {
            try {
                matriculaInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'matricula.label', default: 'Matricula'), params.id])}"
				atualizaNumeroAlunos(matriculaInstance)
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'matricula.label', default: 'Matricula'), params.id])}"
                redirect(action: "list", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'matricula.label', default: 'Matricula'), params.id])}"
            redirect(action: "list")
        }
		atualizaNumeroAlunos(matriculaInstance)
    }
	
	private void atualizaNumeroAlunos(matriculaInstance){
		int turmaId = matriculaInstance.getTurma().getId();
		int totalAlunos = Matricula.countByTurmaAndAtivo(matriculaInstance.getTurma(),TypeAtivo.SIM);
		Turma turma = Turma.get(turmaId);
		turma.setTotalAlunos(totalAlunos); 
		turma.save();
	}
}
